package com.yifeng.repo.base.service.impl;

import com.google.common.base.Strings;
import com.yifeng.base.bds.api.lbs.LbsRestService;
import com.yifeng.base.bds.api.lbs.dto.LocationInfoDto;
import com.yifeng.base.bds.api.lbs.dto.RegionInfoDto;
import com.yifeng.base.bds.api.lbs.params.GeoInfoParam;
import com.yifeng.base.bds.api.lbs.params.RegionInfoParam;
import com.yifeng.repo.base.params.ConsoleParam;
import com.yifeng.repo.base.security.proxy.CommonProxy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daibing on 2024/3/1.
 */
public class LbsRestServiceImpl extends CommonProxy implements LbsRestService {
    private final String mapping;

    public LbsRestServiceImpl(ConsoleParam consoleParam) {
        super(consoleParam);
        this.mapping = "/yfcloud/bds/rest/LbsRestService";
    }

    @Override
    public List<RegionInfoDto> listRegionInfo(RegionInfoParam param) {
        String url = consoleParam.getUrl() + mapping + "/listRegionInfo";
        Map<String, String> params = new HashMap<>(4);
        params.put("regionLevel", param.getRegionLevel() != null ? param.getRegionLevel().toString() : null);
        params.put("parentCode", Strings.isNullOrEmpty(param.getParentCode()) ? null : param.getParentCode());
        params.put("offsetRegionCode", param.getOffsetRegionCode());
        params.put("pageSize", param.getPageSize() != null ? param.getPageSize().toString() : null);
        String queryParameters = buildQueryParameters(params);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, RegionInfoDto.class);
    }

    @Override
    public LocationInfoDto getLocationInfoByGeo(GeoInfoParam param) {
        String url = consoleParam.getUrl() + mapping + "/getLocationInfoByGeo";
        Map<String, String> params = new HashMap<>(2);
        params.put("longitude", param.getLongitude().toString());
        params.put("latitude", param.getLatitude().toString());
        String queryParameters = buildQueryParameters(params);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, LocationInfoDto.class);
    }

    @Override
    public LocationInfoDto getLocationInfoByFullAddress(String fullAddress) {
        String url = consoleParam.getUrl() + mapping + "/getLocationInfoByFullAddress";
        String queryParameters = buildSingleQueryParameters("fullAddress", fullAddress);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, LocationInfoDto.class);
    }
}
